(function(window){

var screen = document.getElementById('screen'),
	grids = screen.getElementsByTagName('div');



var 
	mooluFilm = function(obj){
		return new muFilm(obj);
	},
	
	muFilm = function(obj){
		var 
			grids = obj.grids,
			frames = obj.frames;
		
		this.frames = frames;
		
		for(var i=0; i<grids.length; i++){
		    this['grid' + i] = grids[i];
		}
		
	},
	
	aider = {
		
		transcode : function(thirtySixCode){
						
    		return parseInt(thirtySixCode, 36)
				   .toString(2).substr(1);
			
		},
		
		play : function(context){
			var 
				ctx = context,
				frames = ctx.frames,
				section = [],
				strips = [], 
				counter = 0,
				self;
			
			this.ctx = ctx;			
			
			for(var i=0; i<frames.length; i++){
			    section = frames[i].split('-');
				strips.push(section);
			}
			
			if (!this.timeId) {
								
				self  = this;
				
				this.timeId = window.setInterval(function(){
					if(counter == 2)
						counter = 0
					
					self.runFrame.call(self, strips[counter++]);
					
				}, 1000);
			}
			
		},
		
		runFrame : function(frame){
			
			var 
				section = [],
				strip;
		
			for(var i=0; i<frame.length; i++){
			    section[i] = this.transcode(frame[i]);
			}
			
			strip = section.join('');
			this.updateScreen(strip);
		},
		
		updateScreen : function(strip){
			var 
				states = strip.split('');
				
			for(var i=0; i<states.length; i++){
			    this.updateElState(states[i], i);
			}
			
		},
		
		updateElState : function(state, i){
		
			var 
				ctx = this.ctx,
				color;
			
			if(state == 1)
				color = 'rgb(128, 128, 128)';
			else
				color = 'rgb(192, 192, 192)';
					
			ctx['grid' + i].style.backgroundColor = color;
		}
	};
	
	
	
	
muFilm.prototype = {
	show : function(prop){
		var 
			cfg = {
				prop: prop
			},
			len = arguments.length,
			p, param;				
				
		for(var i=1; i<len; i++){
			param = arguments[i];
			switch(typeof param) {
				case "number":
					
					break;
				
				case "string":
					
					break;
					
				case "function":
					
					break;
				
				case "object":
					for(p in param) {
						cfg[p] = param[p];
					}			
			}
		}
		
		aider.play(this);
		
		return this;
	}
};

/*

var g0 = '000000000000000000000000000000';
var g0 = '111111111111111111111111111111';
var g1 = '000000000000000000000000000000';
var g2 = '000000000000000000000000000000';
var g3 = '000000000000000000000000000000';
var g4 = '000000000000000000000000000000';
var g5 = '000000000000000000000000000000';
var g6 = '000000000000000000000000000000';
var g7 = '000000000000000000000000000000';
var g8 = '000000000000000000000000000000';
var g9 = '000000000000000000000000000000';
var g10 = '000000000000000000000000000000';
var g11 = '000000000000000000000000000000';
var g12 = '000000000000000000000000000000';
var g13 = '000000000000000000000000000000';
var g14 = '000000000000000000000000000000';
var g15 = '000000000000000000000000000000';
var g16 = '000000000000000000000000000000';
var g17 = '000000000000000000000000000000';
var g18 = '000000000000000000000000000000';
var g19 = '000000000000000000000000000000';

var gg = ['hra0hs', 'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs',
		  'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs',
		  'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs',
		  'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs', 'hra0hs'],

	bb = ['zik0zj', 'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj',
		  'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj',
		  'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj',
		  'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj', 'zik0zj'],

	nn = 'hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-' + 
		 'hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-' + 
		 'hra0hs-hra0hs-hra0hs-hra0hs-hra0hs-hra0hs',
		 
	mm = 'zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-' +
	   	 'zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-' +
		 'zik0zj-zik0zj-zik0zj-zik0zj-zik0zj-zik0zj';

function transcode(binaryOr36Code, flag){
	
	if(flag){
    	return parseInt(binaryOr36Code, 36)
					.toString(2);
	}else{
		return parseInt('1' + binaryOr36Code, 2)
					.toString(36);
	}
}

console.log('1' + g0);

var temp1 = transcode(g0);
console.log(temp1);

var temp2 = transcode(temp1, true);
console.log(temp2);

console.log(bb.join('-'));

*/


var mm = 'tlgotm-nodcnp-tlgotm-nb1w5h-tesyki-nb1w5h-tleo6a-nhpmdx-tlgryq-nhpnyt-tlgqdu-nhpmdx-tlgosy-nfgusl-tesyki-nb1w5h-tesyki-nodcnp-tlgotm-nodcnp',
		 
	nn = 'tlgotm-nodcnp-tlgotm-nb1w5x-tesykq-nb1w5x-ti6psa-nectg5-ti3vuy-nfokr9-ti3vuy-nectg5-ti6psa-nb1w5x-tesykq-nb1w5x-tesykq-nodcnp-tlgotm-nodcnp';
		 
var cards = [nn, mm];

var o = {};
o.grids = grids;
o.frames = cards;
mooluFilm(o).show();
})(window);